<?php
namespace App\Service;
use App\Lib\Logger;
use Illuminate\Support\Facades\DB;
use App\Models\Admin\Post as PostModel;
use App\Models\Admin\PostGroup as PostGroupModel;
class PostService extends BaseService{

    /*
     * 列表
     */
    public function getPostList($search) {
        return PostModel::getList(isset($search['limit'])?$search['limit']:15,$search);
    }
    /*
     * 创建单条数据
     */
    public function createData($data){
        $data['creator'] = getCurrentUser()["id"];
        $result = PostModel::create($data);
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "新增岗位[ID:".$result['id']."],成功";
        }
        $this->log($level,Logger::POST,'create',$message,$result);
        return $result;
    }
    /*
     * 创建批量数据
     */
    public function createPostData($data){
        $status = [];
        DB::beginTransaction();
        foreach ($data["group"] as $k=>$v){
            $arr[$k]["group_id"] = $data["group_id"];
            $arr[$k]["name"] = $v;
        }
        foreach ($arr as $k=>$v){
            $result = $this->createData($v);
            if($result){
                $status[] = 1;
            }else{
                $status[] = -1;
            }
        }
        if(array_search(-1,$status)){
            DB::rollBack();
            return -1;
        }else{
            DB::commit();
            return 1;
        }
    }
    /*
     * 查询单条数据
     */
    public function findData($id){
        return PostModel::find($id);
    }
    /*
     * 修改单条数据
     */
    public function update($id,$data){
        $result = PostModel::where("id",$id)->update($data);
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "修改岗位[ID:".$id."],成功";
        }
        $this->log($level,Logger::POST,'update',$message,$data);
        return $result;
    }
    /*
     * 删除单条数据
     */
    public function delete($id){
        $result = PostModel::where("id",$id)->delete();
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "删除成功";
        }
        $this->log($level,Logger::POST,'destroy',$message,['del_id'=>$id]);
        return $result;
    }
    public function changeSort($data){
        return $this->update($data['id'],['sort'=>$data['value']]);
    }
    /*
     * 查询岗位分组数据
     */
    public function getGroupList($search){
        return PostGroupModel::getList(isset($search['limit'])?$search['limit']:15,$search);
    }
    /*
     * 创建岗位分组
     */
    public function createGroup($data){
        $data['creator'] = getCurrentUser()["id"];
        $data['site_id'] = getCurrentSiteId();
        $result = PostGroupModel::create($data);
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "新增岗位分组[ID:".$result['id']."],成功";
        }
        $this->log($level,Logger::POST,'create',$message,$result);
        return $result;
    }
    /*
     * 更新岗位分组
     */
    public function updateGroup($id,$data){
        $result = PostGroupModel::where("id",$id)->update($data);
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "修改岗位分组[ID:".$id."],成功";
        }
        $this->log($level,Logger::POST,'update',$message,$data);
        return $result;
    }
    /*
     * 查询单条岗位分组
     */
    public function findGroupData($id){
        return PostGroupModel::find($id);
    }
    /*
     * 删除岗位分组
     */
    public function groupDelete($id){
        $result = PostGroupModel::where("id",$id)->delete();
        if($result){
            $level = Logger::LOG_LEVEL_INFO;
            $message = "删除成功";
        }
        $this->log($level,Logger::POST,'destroy',$message,['del_id'=>$id]);
        return $result;
    }
    /*
     * 查询岗位数据
     */
    public function selectData($where = []){
        return PostModel::where($where)->get();
    }
    /*
     * 岗位分组排序
     */
    public function changeGroupSort($data){
        return $this->updateGroup($data['id'],['sort'=>$data['value']]);
    }
    /*
     * 查询本站所有岗位
     */
    public function selectSitePost(){
        $ids = PostGroupModel::pluck('id');
        return PostModel::whereIn("group_id",$ids)->get();
    }
}
